#!/usr/bin/python2.7

"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
Author   : Fami
Email    : gravitydevelopment@gmail.com
Scripts  : utils.py 
Version  : 0.1
Date     : 3 Nov 2014
Desc     : Consist utilities function
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
import os
import shutil

#000-Function: Testing Purpose
def printOut(inputMsg):
    print inputMsg


#001-Function: CHECK FOLDER
def checkFolder (folderPath):
    try:
        returnVal = (os.path.isdir(folderPath))
    except Exception, e:
        print "utils-checkFolder ERROR:\n"+e
        exit(2)

    if str(returnVal) == 'True':
        print "utils-checkFolder INFO: Folder Check- Existed!\n"
    else:
        print "utils-checkFolder WARNING: Folder Check- Not Exist!\n"
    return returnVal


#002-Function: CHECK FILE
def checkFile(folderPath, filePath):
    try:
        returnVal = (os.path.isfile(folderPath+'/'+filePath))
    except Exception, e:
        print "utils-checkFolder ERROR:\n"+str(e)
        exit(2)   

    if str(returnVal) == 'True':
        print "utils-checkFolder INFO: File Check- Existed.\n"
    else:
        print "utils-checkFolder WARNING: File Check- Not Exist.\n"
    return returnVal


#003-Function: COPY FILE
def copyFile(srcFolderPath, destFolderPath, fileName):
    pathList = [srcFolderPath, destFolderPath]
    countVal = 0

    for pathCheck in pathList:
        try:
            returnFolderFlag = checkFolder(pathCheck)

            if str(returnFolderFlag) == 'True':
                countVal = countVal + 1
                print countVal
            else:
                exit(2)
        except Exception, e:
            print "utils-copyFile ERROR:\n"+str(e)
            exit(2)
   
    if int(countVal) == int('2'):
        try:
            returnFileFlag = checkFile(srcFolderPath, fileName)
            if str(returnFileFlag)=='True':
                sourceDir = srcFolderPath + fileName
                shutil.copy(sourceDir, destFolderPath)
                returnFileFlag = checkFile(destFolderPath, fileName)
                if str(returnFileFlag) == 'True':
                    print 'utils-copyFile INFO: File '+fileName+' successfully copied.\n'
                    print '                     Destination Folder:'+ destFolderPath+'\n'
                    exit(2)
            else:
                print 'utils-copyFile FAILURE: Copy Execution Failed!\n'
                exit(2)
        except Exception, e:
            print "utils-copyFile ERROR:\n"+str(e)
            exit(2)
    else:
        print 'utils-copyFile FAILURE: Copy Execution Fail due to unexisted folder.\n'
        
        
#004-Function: COPY FOLDER
def copyFolder(srcFolderPath, destFolderPath, folderName):
    pathList = [srcFolderPath, destFolderPath]
    countVal = 0

    for pathCheck in pathList:
        try:
            returnFolderFlag = checkFolder(pathCheck)
            if str(returnFolderFlag) == 'True':
                countVal = countVal + 1
                print countVal
            else:
                exit(2)
        except Exception, e:
            print "utils-copyFolder ERROR:\n"+str(e)
            exit(2)
   
    if int(countVal) == int('2'):
        try:
            shutil.move(srcFolderPath, destFolderPath)
            copyFolderPath = destFolderPath+folderName
            #check folder will fail due to the attribute point of the folderName point to the file name in the copied folder
            #returnFileFlag = checkFolder(copyFolderPath)
            if str(returnFileFlag) == 'True':
                    print 'utils-copyFolder INFO: Folder '+folderName+' successfully copied.\n'
                    print '                     Destination Folder:'+ destFolderPath+'\n'
                    exit(2)
            else:
                print 'utils-copyFile FAILURE: Folder copy Execution Failed!\n'
                exit(2)              

        except Exception, e:
            print "utils-copyFolder ERRORafsfa:\n"+str(e)
            exit(2)
    else:
        print 'utils-copyFile FAILURE: Copy Execution Fail due to unexisted folder.\n'


#005-Function: LIST DIR IN FOLDER
def listDir(folderPath, keyword):
    try:
        returnFolderFlag = checkFolder(folderPath)
        if str(returnFolderFlag) == 'True':
            matchObj = re.match(r'\.', keyword)
            if matchObj:
                fileList = os.listdir(folderPath)
                for file in fileList:
                    print file
            elif matchObj:
                fileList = os.listdir(folderPath)
                for file in fileList:
                    print file
        else:
            exit(2)
    except Exception, e:
        print "utils-copyFile ERROR:\n"+str(e)
        exit(2)


##006-LIST FILE IN FOLDER  BASED ON EXT###

##007-LIST FILE IN FOLDER  BASED ON KEYWORD###

    
